package rabbit.common;

import com.rabbitmq.client.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringWriter;
import java.util.concurrent.TimeoutException;

/**
 * Created by root on 17-4-4.
 */
@Component
public class rabbitMqHelper {
    private static Logger LOGGER = Logger.getLogger("rabbit.common.rabbitMqHelper");

    private ConnectionFactory connectionFactory ;
    private Connection connection;
    //TODO to be declared in configuration file
    private String queueName = "helloWorld";

    public rabbitMqHelper(){
        initConnectionFactory("localhost",10);
    }
    public rabbitMqHelper(String host,int netWorkRecoveryInterval){
        initConnectionFactory(host,netWorkRecoveryInterval);
    }
    public rabbitMqHelper(String configId){
        initConnectionFactory(configId);
    }


    public void initConnectionFactory(String host,int netWorkRecoveryInterval){
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        //connectionFactory.setHandshakeTimeout(5*1000);
        //connectionFactory.setAutomaticRecoveryEnabled(true);
        connectionFactory.setNetworkRecoveryInterval(netWorkRecoveryInterval);
    }


    public void initConnectionFactory(String mqConfigName){
        if(rabbitMqConfigBundle.getConfigBundle().getConfigMap().containsKey(mqConfigName)){
            rabbitMqConfig config = rabbitMqConfigBundle.getConfigBundle().getConfigMap().get(mqConfigName);
            connectionFactory = new ConnectionFactory();
            connectionFactory.setHost(config.getHost());
            //connectionFactory.setHandshakeTimeout(5*1000);
            //connectionFactory.setAutomaticRecoveryEnabled(true);
            connectionFactory.setNetworkRecoveryInterval(config.getNetWorkRecoveryInterval());


        }
        else {
            LOGGER.info("config cannot be found, please check the config file");
            throw new IllegalArgumentException("config cannot be found, please check the config file");
        }
    }


    public Channel createChannel() throws IOException,TimeoutException{
        if(this.connection==null){
            this.connection = connectionFactory.newConnection();
        }
        return connection.createChannel();

    }

    public Connection createConnection()throws IOException,TimeoutException{
        if(this.connection==null){
            this.connection = connectionFactory.newConnection();
        }
        return this.connection;
    }

    public void sendMessage(String message)throws IOException,TimeoutException{
       Channel channel =  createChannel();
       queueDeclare(channel,true,false, false);
       //AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(2).build();

       channel.basicPublish("",this.queueName,null,message.getBytes());
       channel.close();
    }

    public void sendMessage(Channel channel,String message)throws IOException,TimeoutException{
        queueDeclare(channel,true,false, false);
        channel.basicPublish("",this.queueName,null,message.getBytes());
        channel.close();
    }

    public void queueDeclare(boolean queueDurable, boolean exlusive, boolean autoDelete)throws IOException,TimeoutException{
        Channel channel =  createChannel();
        //TODO make queue more parametrable
        queueDeclare(channel,queueDurable,exlusive,autoDelete);
    }

    public void queueDeclare(Channel channel, boolean queueDurable, boolean exlusive, boolean autoDelete)throws IOException,TimeoutException{
        //TODO make queue more parametrable
        channel.queueDeclare(this.queueName,queueDurable,exlusive,autoDelete,null);
    }

    public void basicConsumeMessage(Channel channel, Consumer consumer) throws IOException {
        channel.basicConsume(this.queueName,true, consumer);
    }

    public void closeConnection()throws IOException{
        if(this.connection!=null){
            this.connection.close();
        }
    }
}
